package com.espirit.eap.sa;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import com.googlecode.cswish.model.Saas;

/**
 * It's the Page permission which contains field permission
 */
@Entity
public class UserTemplatePrivilege implements Serializable, Saas {
	private static final long serialVersionUID = 976836652900540860L;
	
	private Integer id;
	private UserTemplate userTemplate;
	private Privilege privilege;
	private List<PageFieldPermission> fieldPermissions;
	
	private Integer sid;
	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}
	
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	@ManyToOne(targetEntity=UserTemplate.class)
	public UserTemplate getUserTemplate() {
		return userTemplate;
	}
	
	public void setUserTemplate(UserTemplate userTemplate) {
		this.userTemplate = userTemplate;
	}
	
	@ManyToOne(targetEntity=Privilege.class)
	public Privilege getPrivilege() {
		return privilege;
	}
	public void setPrivilege(Privilege privilege) {
		this.privilege = privilege;
	}
	
	@OneToMany(targetEntity=PageFieldPermission.class ,orphanRemoval = true,cascade = CascadeType.ALL,mappedBy="userTemplatePrivilege",fetch=FetchType.LAZY)
	public List<PageFieldPermission> getPageFieldPermissions() {
		return fieldPermissions;
	}
	public void setPageFieldPermissions(List<PageFieldPermission> pageFieldPermissions) {
		this.fieldPermissions = pageFieldPermissions;
	}
	public List<PageFieldPermission> safeFieldPermissions() {
		if (fieldPermissions == null){
			fieldPermissions = new ArrayList<PageFieldPermission>();
		}
		return fieldPermissions;
	}
}